package com.yinhe.music.yhmusic.main.my;

import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.MyAdapter;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.model.HeaderBean;
import com.yinhe.music.yhmusic.model.MyTitleItem;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.model.UserInfo;
import com.yinhe.music.yhmusic.personal.PersonPresenter;
import com.yinhe.music.yhmusic.utils.Preferences;

import java.util.ArrayList;
import java.util.List;

/**
 * HomePageFragment的P层
 *
 * @author 何晓华
 */
public class MyPresenter extends PersonPresenter<MyModel, MyContract.IMyView> implements MyContract.IMyPresenter {
    private int[] icon = {R.drawable.icon_local, R.drawable.icon_down,
            R.drawable.icon_recent, R.drawable.icon_collect,
            R.drawable.ic_my_radio, R.drawable.icon_music};

    private int[] count = new int[6];
    private String[] iconName = {"本地歌曲", "下载管理", "最近播放", "我的收藏", "我的电台", "已购歌曲"};

    void parseData(UserInfo userInfo) {
        ArrayList<MultiItemEntity> res = new ArrayList<>();
        MyTitleItem item1 = new MyTitleItem("我创建的歌单", "0");
        MyTitleItem item2 = new MyTitleItem("我收藏的歌单", "0");

        List<SongMenuList> createList = userInfo.getCreateSongMenu();
        initSongMenuList(createList, item1);
        mModel.doSyn(createList, IConstants.CREATE_MENU);

        List<SongMenuList> collectList = userInfo.getCollectSongMenu();
        initSongMenuList(collectList, item2);
        mModel.doSyn(collectList, IConstants.COLLECT_MENU);

        res.add(item1);
        res.add(item2);

        getView().setMenuUI(res);
    }

    /**
     * 设置item数据
     *
     * @param mList 歌单信息
     * @param item  see{@link MyTitleItem}
     */
    private void initSongMenuList(List<SongMenuList> mList, MyTitleItem item) {
        for (SongMenuList menu : mList) {
            menu.setItemType(MyAdapter.TYPE_ITEM);
            item.addSubItem(menu);

            if (menu.getPrivateX() == 1) {
                Preferences.setFavMenu(menu.getSongMenuId());
            }
        }

        item.setCount(String.valueOf(mList.size()));
    }

    @Override
    public List<HeaderBean> getHeaderData() {
        List<HeaderBean> list = new ArrayList<>();
        count[1] = (int) DataBaseAccessor.getInstance().getDownMusicCount();
        count[2] = (int) DataBaseAccessor.getInstance().getRecentCount();
        for (int i = 0; i < icon.length; i++) {
            HeaderBean headerBean = new HeaderBean(iconName[i], count[i], icon[i]);
            list.add(headerBean);
        }
        return list;
    }

    @Override
    public List<MultiItemEntity> getLocalMenuData() {
        return mModel.getLocalMenuData();
    }

    @Override
    public void getShowMoreData(int position, BaseQuickAdapter mAdapter) {
        List<SongMenuList> editList = new ArrayList<>();
        if (!Preferences.getLoginSn().isEmpty()) {
            if (position == 0) {
                MyTitleItem titleItem = (MyTitleItem) mAdapter.getData().get(position);
                List<SongMenuList> createList = titleItem.getSubItems();

                if (createList == null || createList.isEmpty()) {
                    return;
                }
//                Log.e("看来是的后果是快乐的韩国", JsonUtil.toString(createList));
                for (int i = 0; i < createList.size(); i++) {
                    if (createList.get(i).getPrivateX() == 2) {
                        editList.add(createList.get(i));
                    }
                }
                getView().showManagerDialog(editList, IConstants.CREATE_MENU);
            } else {
                MyTitleItem titleItem = (MyTitleItem) mAdapter.getData().get(position);
                List<SongMenuList> collectList = titleItem.getSubItems();
                getView().showManagerDialog(collectList, IConstants.COLLECT_MENU);
            }
        } else {
            getView().startLogin();
        }
    }

    @Override
    public void getLocalVisitorData() {
        if (getView() == null) {
            return;
        }
        getView().setLocalMenuUI(mModel.getLocalVisitorData());
    }
}
